import { useMount, useRequest } from 'ahooks';
import { Button, message } from 'antd';

import { editAttr, findByAtterList } from '@/services/sys/attribute';
import { PlusOutlined } from '@ant-design/icons';
import { BetaSchemaForm, ProFormInstance } from '@ant-design/pro-form';
import { ProFormColumnsType } from '@ant-design/pro-form/es/components/SchemaForm/typing';
import ProTable from '@ant-design/pro-table';
import { SetStateAction, useRef, useState } from 'react';
import { history } from 'umi';

const columnDialog: {
  title: string;
  valueType: string;
  columns: (
    | {
        title: string;
        dataIndex: string;
        key: string;
        initialValues: number;
        hideInForm: boolean;
      }
    | {
        title: string;
        dataIndex: string;
        key: string;
        width: string;
      }
    | { title: string; dataIndex: string; width: string }
    | {
        title: string;
        dataIndex: string;
        valueType: string;
        width: string;
      }
  )[];
}[] = [
  {
    title: '基本信息',
    valueType: 'group',
    columns: [
      {
        title: 'id',
        dataIndex: 'id',
        key: 'id',
        initialValues: 0,
        hideInForm: true,
      },
      {
        title: '名称',
        dataIndex: 'name',
        key: 'name',
        width: 'md',
      },
      {
        title: '关键字',
        dataIndex: 'iskey',
        width: 'md',
      },
      {
        title: '备注',
        dataIndex: 'remark',
        valueType: 'textarea',
        width: 'md',
      },
    ],
  },
];

const cloneTbale = () => {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const formRef = useRef<ProFormInstance>();
  const column: ProFormColumnsType<API.Attribute> = [
    {
      title: '编号',
      dataIndex: 'index',
      key: 'index',
      valueType: 'indexBorder',
    },
    {
      title: '名称',
      dataIndex: 'name',
      key: 'name',
      width: 'md',
    },
    {
      title: '关键字',
      dataIndex: 'iskey',
      width: 'md',
    },
    {
      title: '备注',
      dataIndex: 'remark',
    },
    {
      title: '操作',
      width: '164px',
      key: 'option',
      valueType: 'option',
      render: (_: any, row: any, index: any, action: any) => [
        <a
          key="link"
          onClick={(a) => {
            console.log(row);
            editDialogInfo(row, index);
          }}
        >
          编辑
        </a>,
        <a
          key="link"
          onClick={(a) => {
            console.log(row);

            history.push(
              {
                pathname: 'values',
              },
              row,
            );
          }}
        >
          详细
        </a>,
      ],
    },
  ];
  const [editDialg, setEditDialg] = useState(false);
  const [attribute, setAttribute] = useState<API.Attribute>();
  const {
    run: fetchByAtterList,
    refresh,
    data: dataList,
    loading,
  } = useRequest(findByAtterList, {
    debounceWait: 100,
    manual: true,
  });

  function editDialogInfo(
    row: SetStateAction<API.Attribute | undefined>,
    index: any,
  ) {
    setAttribute(row);
    formRef?.current?.setFieldsValue(row);
    setEditDialg(true);
  }

  function hideEditDialg() {
    setAttribute({ id: 0 });
    formRef.current?.resetFields();
    setEditDialg(false);
  }

  function openEditDialg() {
    // @ts-ignore
    setAttribute({ id: 0 });
    formRef.current?.resetFields();
    setEditDialg(true);
  }

  useMount(async () => {
    fetchByAtterList();
  });

  return (
    <div>
      <BetaSchemaForm<API.Attribute>
        formRef={formRef}
        initialValues={attribute}
        onCancel={() => setEditDialg(false)}
        onVisibleChange={setEditDialg}
        title="字典管理"
        visible={editDialg}
        layoutType="ModalForm"
        columns={columnDialog}
        onFinish={async (values) => {
          // @ts-ignore
          values.id = attribute?.id;
          console.log(values);
          return new Promise<boolean>((resolve, reject) => {
            editAttr(values)
              .then(() => {
                message.success('操作成功');
                setEditDialg(false);
                refresh();
                resolve(true);
              })
              .catch(() => {
                message.success('操作失败');
                reject(false);
              });
          });
        }}
      />
      <ProTable<API.Attribute>
        search={false}
        toolBarRender={() => [
          <Button
            onClick={() => {
              formRef.current?.resetFields();
              openEditDialg();
            }}
            key="button"
            icon={<PlusOutlined />}
            type="primary"
          >
            新建
          </Button>,
        ]}
        onDataSourceChange={(d) => {
          console.log('onDataSourceChange', d);
        }}
        onLoad={(d) => {
          // 数据加载完操作
          console.log('onDataSourceChange', d);
        }}
        dataSource={dataList}
        loading={loading}
        columns={column}
      ></ProTable>
    </div>
  );
};

export default cloneTbale;
